package com.xbts.common.utils.reflect;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
public class GenericSuperclassUtil {
 
    public static Class<?> getActualTypeArgument(Class<?> clazz) {
        Class<?> entitiClass = null;
        Type genericSuperclass = clazz.getGenericSuperclass();
        if (genericSuperclass instanceof ParameterizedType) {
            Type[] actualTypeArguments = ((ParameterizedType) genericSuperclass)
                    .getActualTypeArguments();
            if (actualTypeArguments != null && actualTypeArguments.length > 0) {
                entitiClass = (Class<?>) actualTypeArguments[0];
            }
        }
        return entitiClass;
    }

    /**
     * 获取泛型的第i个参数
     * @param clazz
     * @param i
     * @return
     */
    public static Class<?> getActualTypeArgument(Class<?> clazz,int i) {
        Class<?> entitiClass = null;
        Type genericSuperclass = clazz.getGenericSuperclass();
        if (genericSuperclass instanceof ParameterizedType) {
            Type[] actualTypeArguments = ((ParameterizedType) genericSuperclass)
                    .getActualTypeArguments();
            if (actualTypeArguments != null && actualTypeArguments.length > 0) {
                entitiClass = (Class<?>) actualTypeArguments[i];
            }
        }
        return entitiClass;
    }

    public static void tt(Class<?> clazz){
        ParameterizedType parameterizedType = (ParameterizedType) clazz.getGenericSuperclass();
        Class<?> rawType = (Class<?>) parameterizedType.getRawType();
        Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();

        System.out.println("Raw Type: " + rawType.getName());
        for (Type actualTypeArgument : actualTypeArguments) {
            System.out.println("Actual Type Argument: " + actualTypeArgument.getTypeName());
        }
    }
}